package org.jeecg.modules.flower.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.chvegetable.service.IFlowerTagBookService;
import org.jeecg.modules.flower.entity.FlowerOutorder;
import org.jeecg.modules.flower.entity.FlowerOutorderinfo;
import org.jeecg.modules.flower.service.IFlowerOutorderNewService;
import org.jeecg.modules.flower.util.CommonUtil;
import org.jeecg.modules.flower.vo.FlowerOutorderPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 出库单表---新新新新新新    ----g  给销售系统专用接口
 *
 * @author gf
 * 2023-3-13 10:01
 * @descriptionauthor
 * @return
 */
@Api(tags = "销售系统接口-新出库单")
@RestController
@RequestMapping("/outOrderForSeal/flowerOutorder")
@Slf4j
public class FlowerOutorderNewController extends JeecgController<FlowerOutorder, IFlowerOutorderNewService> {
    @Autowired
    private IFlowerOutorderNewService flowerOutorderService;
    @Autowired
    private IFlowerTagBookService flowerTagBookService;


    /**
     * 确认出库
     *
     * @param flowerOutorderPage
     * @return
     */
    @AutoLog(value = "确认出库----")
    @ApiOperation(value = "确认出库----", notes = "确认出库----状态（-1待出库，0已出库，1删除，2撤销）")
    @PutMapping(value = "/sureOut")
    public Result<?> sureOut(@RequestBody FlowerOutorderPage flowerOutorderPage) throws IOException {
        int outDo = 0;
        List<FlowerOutorderinfo> infoListsForTnumber = flowerOutorderPage.getFlowerOutorderinfoList();
        String id = flowerOutorderPage.getId();
        synchronized (CommonUtil.OutOrderLockKey) {
            outDo = flowerOutorderService.sureOutDo(id, infoListsForTnumber);
        }
        return Result.ok("库管确认成功" + outDo + "扎");
    }

    /**
     * 返回到待出库
     *
     * @param id
     * @return
     */
    @AutoLog(value = "确认出库-撤销--")
    @ApiOperation(value = "确认出库撤销---", notes = "出库单表-撤销确认出库---")
    @GetMapping(value = "/toWaitOut")
    public Result<?> toWaitOut(@RequestParam(name = "id", required = true) String id) throws IOException {
        synchronized (CommonUtil.OutOrderLockKey) {
            flowerOutorderService.sureOutDoBack(id);
        }
        return Result.ok("撤销确认出库成功!");
    }


    //冲红
    @ApiOperation(value = "冲红-- ", notes = "冲红--冲红 ")
    @PostMapping(value = "/redOutOrderByNos")
    public Result<?> redOutOrderByNos(@RequestParam(name = "orderBo") String orderBo) throws IOException {
        synchronized (CommonUtil.OutOrderLockKey) {
            flowerOutorderService.redOutOrderByNos(orderBo, null, null, null);
        }
        return Result.ok("冲红成功");
    }

    @AutoLog(value = "出库单 结账")
    @ApiOperation(value = "出库单 结账", notes = "出库单 结账")
    @PostMapping(value = "/settleAccounts")
    @Transactional(rollbackFor = Exception.class)
    public Result<?> settleAccounts( FlowerOutorder flowerOutorder,
                                     @RequestParam(value = "period" ,required = true) String period,
                                     @RequestParam(value = "susDate", required = true) String susDate,
                                     @RequestParam(value = "sueDate", required = true) String sueDate,
                                     HttpServletRequest req) {
        UpdateWrapper<FlowerOutorder> queryWrapper = new UpdateWrapper<>();
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();

        if (StringUtils.isNotBlank(susDate)) {
            queryWrapper.ge("topresstime", susDate);
        }
        if (StringUtils.isNotBlank(sueDate)) {
            queryWrapper.le("topresstime", sueDate + " 23:59:59");
        }
//        queryWrapper.inSql("id", "SELECT a.order_id FROM t_flower_outorderinfo a LEFT JOIN   flower_storehouse b  on  a.storehouse = b.code  LEFT JOIN   flower_storehouse_detail c  on b.id =c.mainid WHERE  personid = '" + sysUser.getId() + "'");
        queryWrapper.ne("period_state", "已结账");
        queryWrapper.set("period", period);
        queryWrapper.set("period_state", "已结账");
        queryWrapper.set("period_person", sysUser.getRealname());
        queryWrapper.set("period_time",  new Date());
        long count = flowerOutorderService.count(queryWrapper);
        boolean update = flowerOutorderService.update(queryWrapper);

        if (update) {
            return Result.ok("结账成功" + count + "出库单");
        } else {
            return Result.ok("结账失败");
        }
    }
}
